package com.example.batchdemo.controller;

import com.example.batchdemo.model.BulkAdjustment;
import com.example.batchdemo.service.BulkAdjustmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author jason.tang
 * @Date 2019-10-06 22:06
 * @Desc
 */

@RestController
@RequestMapping("/batch")
public class BatchController {

    @Autowired
    private BulkAdjustmentService bulkAdjustmentService;

    @ApiOperation(value = "上传批量更新数据文件", notes = "上传批量更新数据文件")
    @PostMapping(value = "/file")
    public BulkAdjustment uploadParticipantDataFile(@RequestParam("file") MultipartFile file,
                                                    @RequestParam("fileType") String fileType,
                                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (file.isEmpty()) {
            throw new RuntimeException("Upload File Is Empty!");
        }
        return bulkAdjustmentService.uploadDataFile(file.getBytes(), fileType, file.getOriginalFilename());
    }

    @ApiOperation(value = "提交批量更新数据", notes = "提交批量更新数据")
    @PostMapping
    public BulkAdjustment submitParticipantData(@RequestBody BulkAdjustment bulkAdjustment,
                                                HttpServletRequest request, HttpServletResponse response) throws Exception {
        return bulkAdjustmentService.submitBulkAdjData(bulkAdjustment);
    }

}
